// components/SearchInput/SearchInput.js
const app = getApp(), util = require("../../utils/util.js");
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    btnShow: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    disabled: false,
    iptValue: '',
    iptPlaceholder: '搜索',
    left:'30rpx'
  },
  /**
   * 组件的方法列表
   */
  methods: {
    toParking() {
      if (getCurrentPages()[getCurrentPages().length - 1].route.search('parking') == -1) {
        wx.switchTab({
          url: '/pages/parking/index',
        })
      }
    },

    searchParking(e) {
      console.log(e, '搜索')
      let that = this, curPage = getCurrentPages()[getCurrentPages().length - 1],
        data = e.type == 'submit' ? e.detail.value : { parkingName: e.detail.value };
      wx.request({
        url: app.globalData.basicUrl + 'system/parking/list',
        data: data,
        success(res) {
          console.log(res, '查询结果')
          if (res.data.code == 200) {
            res.data.rows.map(item => {
              item.distance = Number(util.distance(curPage.data.localLat, curPage.data.localLon, item.parkingLatitude, item.parkingLongitude))
            })
            res.data.rows.sort(curPage.compare("distance", true))
            curPage.setData({
              indexData: res.data.rows
            })
            that.setData({
              iptValue: ''
            })
            if (data.parkingName) {
              that.setData({
                iptPlaceholder: '点击搜索显示全部数据'
              })
            } else {
              that.setData({
                iptPlaceholder: '搜索'
              })
            }
          }
        }
      })
    }
  }
})
